package com.hzbank.aaronkotlin

import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup.LayoutParams
import com.hzbank.aaronkotlin.databinding.LayoutTextDialogBinding

class InputTextDialog: Dialog {

    var bind: LayoutTextDialogBinding? = null
    private var startX: Int = 0
    private var startY: Int = 0

    private var inputCallback: InputCallback? = null

    constructor(context: Context, startX: Int, startY: Int): super(context){

        this.startX = startX
        this.startY = startY
        initView(context)
    }


    private fun initView(context: Context){

        try{

            bind = LayoutTextDialogBinding.inflate(LayoutInflater.from(context))
            setContentView(bind!!.root)
            setCanceledOnTouchOutside(false)
            parseView()

            //界面大小
            val lp = this.window?.attributes
            lp!!.width = LayoutParams.WRAP_CONTENT
            lp!!.height = LayoutParams.WRAP_CONTENT

            lp!!.x = this.startX
            lp!!.y = this.startY

            lp!!.alpha = 0.5F
            lp!!.dimAmount = 0.5F

            lp!!.verticalMargin = 0.0F
            lp!!.horizontalMargin = 0.0F

            this.window!!.attributes = lp

        }catch (e: Exception){
            e.printStackTrace()
        }
    }

    fun showLoding(){

        if(this.isShowing){
            hiddenLoading()
        }
        show()
    }

    fun hiddenLoading(){

        try{

            this.dismiss()
            this.cancel()
            bind = null;

        }catch (e: Exception){
            e.printStackTrace()
        }

    }

    fun setInputCallback(inputCallback: InputCallback){

        this.inputCallback = inputCallback
    }

    private fun parseView() {

        bind?.inputLabel?.setText("")

        bind?.sureL?.setOnClickListener {

            //确认标准
            val label = bind?.inputLabel?.text.toString()

            if(TextUtils.isEmpty(label)){

                "请输入标注".toast(context)
            } else{

                this.inputCallback?.markLabel(label)

                //bind?.inputLabel?.setText("")

                hiddenLoading()
            }

        }
    }

    companion object{

        interface InputCallback{
            fun markLabel(label: String)
        }

    }
}